package com.aixingfu.sports.view;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.graphics.Typeface;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
import android.widget.TextView;

public class SideBar extends View {

    /*绘制的列表导航字母*/
    private String words[] = {"A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N",
            "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z", "#"};
    /*字母画笔*/
    private Paint wordsPaint;
    /*按下字母背景画笔*/
    private Paint bgPaint;
    /*每一个item的宽度*/
    private int wordWidth;
    /*每一个item的高度*/
    private int wordHeight;
    /*手指按下的字母索引*/
    private int touchIndex = -1;
    /*手指按下的字母改变接口*/
    private onWordsChangeListener listener;

    private TextView mTextDialog;

    public SideBar(Context context) {
        this(context, null);
    }

    public SideBar(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public SideBar(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initPaint();
    }

    private void initPaint() {
        wordsPaint = new Paint();
        bgPaint = new Paint();
        bgPaint.setAntiAlias(true);
        bgPaint.setColor(Color.parseColor("#00b0ff"));
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        wordWidth = getMeasuredWidth();
        int height = getMeasuredHeight();
        wordHeight = height / words.length;
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        for (int i = 0; i < words.length; i++) {

            wordsPaint.setTypeface(Typeface.DEFAULT);
            wordsPaint.setAntiAlias(true);
            wordsPaint.setTextSize(DensityUtils.dp2px(getContext(), 14));

            // 是按下的字母则绘制背景
            if (touchIndex == i) {
                canvas.drawCircle(wordWidth / 2, wordHeight / 2 + i * wordHeight, wordHeight / 2, bgPaint);
                wordsPaint.setColor(Color.WHITE);
            } else {
                wordsPaint.setColor(Color.parseColor("#666666"));
            }

            //获取文字的宽高
            Rect rect = new Rect();
            wordsPaint.getTextBounds(words[i], 0, words[i].length(), rect);
            //得到文字的X坐标
            float wordX = wordWidth / 2 - wordsPaint.measureText(words[i]) / 2;
            float wordY = wordHeight / 2 + rect.height() / 2 + wordHeight * i;
            canvas.drawText(words[i], wordX, wordY, wordsPaint);
            wordsPaint.reset();
        }
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN | MotionEvent.ACTION_MOVE:
                float y = event.getY();
                //关键点===获得我们按下的是那个索引(字母)
                int index = (int) (y / wordHeight);
                touchIndex = index;
                //防止数组越界
                if (listener != null && 0 <= touchIndex && touchIndex <= words.length - 1) {
                    //回调按下的字母
                    listener.wordsChange(words[touchIndex]);
                    invalidate();
                    if (mTextDialog != null) {
                        mTextDialog.setText(words[index]);
                        mTextDialog.setVisibility(View.VISIBLE);
                    }
                }
                break;
            case MotionEvent.ACTION_UP:
                touchIndex = -1;
                invalidate();
                if (mTextDialog != null) {
                    mTextDialog.setVisibility(View.GONE);
                }
                break;
        }
        return true;

    }


    /*手指按下了哪个字母的回调接口*/
    public interface onWordsChangeListener {
        void wordsChange(String words);
    }

    /*设置手指按下字母改变监听*/
    public void setOnWordsChangeListener(onWordsChangeListener listener) {
        this.listener = listener;
    }

    public void setTextDialog(TextView tvDialog) {
        this.mTextDialog = tvDialog;
    }
}
